/*
 * @(#)LogInInterceptor.java $version 2012. 7. 16.
 *
 */

package com.mywep.myalimi.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mywep.myalimi.constant.LogInOut;
import com.mywep.myalimi.model.User;
import com.mywep.myalimi.service.UserService;

/**
 * @author 한지웅
 */
public class LogInInterceptor extends HandlerInterceptorAdapter {
	@Autowired
	private UserService userService;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession(false);

		if (session != null) {
			String userId = (String)session.getAttribute(LogInOut.SESSIONUSERID.getName());
			if (StringUtils.isNotBlank(userId)) {
				User userKey = new User();
				userKey.setId(userId);
				request.setAttribute("user", userService.getUserInfo(userKey));
				request.getAttribute("user");
			}

			return true;
		} else {
			response.sendRedirect("/alimiLogin/logPage.alm");
			return false;
		}


	}
}
